package com.beagle.njddapp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.beagle.njddapp.R;
import com.beagle.njddapp.utils.PatternUtil;
import com.beagle.njddapp.utils.StatusBarUtil;
import com.blankj.utilcode.util.ToastUtils;

/**
 * @author hxx
 * @date 2021-03-18 19:05
 */
public class InputActivity extends AppCompatActivity implements View.OnClickListener {
    public static final int NORMAL_SHORT=1;//2-16
    public static final int NORMAL_ID=2;//身份证
    public static final int NORMAL_NUMBER=3;//数字
    public static final int NORMAL_PHONE=4;//手机号
    public static final int NORMAL_LONG=5;//字数多

    private int type;
    private EditText ed;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input);
        StatusBarUtil.immersive(this,getResources().getColor(R.color.color_F8F9FD));
        ed=findViewById(R.id.ed_input);
        ImageView back=findViewById(R.id.common_back_iv);
        TextView right=findViewById(R.id.common_tv_right);
        right.setText("保存");
        right.setOnClickListener(this);
        back.setOnClickListener(this);
        TextView title=findViewById(R.id.commom_title_tv);
        title.setText("输入信息");
        View vIew=findViewById(R.id.common_container);
        vIew.setBackgroundColor(getResources().getColor(R.color.color_F8F9FD));
        type=getIntent().getIntExtra("type",-1);
        String str=getIntent().getStringExtra("data");
        ed.setText(str);
        if (type==NORMAL_SHORT){
            ed.setMaxEms(16);
        }else if (type==NORMAL_NUMBER||type==NORMAL_PHONE){
            ed.setInputType(InputType.TYPE_CLASS_NUMBER);
            ed.setMaxEms(30);
        }else {
            ed.setMaxEms(99);
        }
        showSoftInputFromWindow();

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.common_back_iv:
                onBackPressed();
                break;
            case R.id.common_tv_right:
                if (type!=NORMAL_NUMBER&&ed.getText().toString().length()<2){
                    ToastUtils.showShort("请正确输入");
                    return;
                }
                if (type==NORMAL_NUMBER){
                    int number=Integer.parseInt(ed.getText().toString());
                    if (number<0||number>99){
                        ToastUtils.showShort("请正确输入");
                        return;
                    }
                }
                if (type==NORMAL_PHONE){
                    if (!PatternUtil.isMobile(ed.getText().toString())){
                        ToastUtils.showShort("请输入正确的手机号");
                        return;
                    }
                }else if (type==NORMAL_ID){
                    if (!PatternUtil.isIDNumber(ed.getText().toString())){
                        ToastUtils.showShort("请输入正确的身份证号");
                        return;
                    }
                }
                Intent intent=new Intent();
                intent.putExtra("data",ed.getText().toString());
                setResult(1006,intent);
                finish();
                break;
        }
    }
    private void showSoftInputFromWindow() {
        ed.setFocusableInTouchMode(true);
        ed.requestFocus();
        InputMethodManager inputManager =(InputMethodManager)ed.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(ed, 0);
    }
}
